package vsy.buzzwordbingo.sort;

import java.util.Comparator;

public abstract class OrderedComparator<T> implements Comparator<T> {
	private Order order;
	
	public OrderedComparator(Order order) {
		this.order = order;
	}
	
	protected final int nullCompare(Object o1, Object o2) {
		if (o1 == null && o2 == null)
			return 0;
		else if (o1 == null)
			return 1;
		else if (o2 == null)
			return -1;
		throw new IllegalArgumentException();
	}
	
	public static enum Order {
		ASCENDING(1), DESCENDING(-1);
		
		private int ord;
		private Order(int o) {
			ord = o;
		}
		
		public int getOrd() {
			return ord;
		}
	}

	@Override
	public final int compare(T o1, T o2) {
		if (o1 == null || o2 == null)
			return nullCompare(o1, o2) * order.getOrd();
		return orderedCompare(o1, o2) * order.getOrd();
	}

	protected abstract int orderedCompare(T o1, T o2);
}
